﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Xml;

namespace UAS5.UControls
{
    public partial class frmTranslate : Form
    {
        public string Words { get; set; }
        public bool AutoRun { get; set; }
        public frmTranslate()
        {
            InitializeComponent();
        }

        private void frmTranslate_Load(object sender, EventArgs e)
        {
            cbEnginees.SelectedIndex = 0;
            if (!string.IsNullOrEmpty(this.Words)) tbText.Text = this.Words;
            if (AutoRun) this.Translating();
        }

        private void cbEnginees_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbEnginees.SelectedIndex == -1) return;
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            Translating();
        }
        private void Translating()
        {
            AutoRun = false;
            if (cbEnginees.SelectedIndex == -1) return;
            string api = "http://fanyi.youdao.com/openapi.do?keyfrom=ehaocha&key=1780990229&type=data&doctype=xml&version=1.1&q=" + tbText.Text;

            WebClient wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            string result = wc.DownloadString(api);

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(result);

            tbResult.Text = doc.DocumentElement.ChildNodes[3].FirstChild.InnerText;
        }

        private void lnkWeb_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (cbEnginees.SelectedIndex == -1) return;
            switch (cbEnginees.SelectedItem.ToString())
            {
                case "有道":
                    System.Diagnostics.Process.Start("http://dict.youdao.com/");
                    break;
                case "谷歌":
                    System.Diagnostics.Process.Start("http://translate.google.cn/");
                    break;
            }
        }
    }
}
